

const crypto = require("crypto");   // Node 自带API 

// 加密函数  aes  data 需要加密的字段 
function aesEncrypt(data, key) {
    const cipher = crypto.createCipher('aes192', key);  // 加密函数
    var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;  // 密文  
}

// 解密 
function aesDecrypt(encrypted, key) {
    const decipher = crypto.createDecipher('aes192', key);  // 解密函数 
    var decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;  // 明文 
}

const keys = "WH06daydayup";   //   abc123 ==>  abc123WH06daydayup

exports.aesEncrypt = aesEncrypt;   // 加密
exports.aesDecrypt = aesDecrypt;   // 解密
exports.keys = keys;        // 密钥 




// 封装判断是否 登录  session.username

exports.checkSession = function (req, res, callback) {
    if (req.session.username) {
        callback(req.session.username);
    } else {
        res.send(`<script>alert('session已经过期或者失效,请重新登录');window.location.href='/login'</script>`)
    }
}